<template>
	<view class="uploadImageBox">
		<view style="display: flex;flex-wrap: wrap;">
			<view v-if="video.url" class="imageAddBox">
				<view v-if="!video.upload" class="uploadBox">
					<u-loading-icon color='#fff'></u-loading-icon>
					<view>
						上传中
					</view>
				</view>
				<image :src="video.coverUrl" v-if="!video.upload" />
				<video controls :src="video.url" object-fit='cover' v-else />
				<view class="del" v-if="video.upload" @click="del(video.id)">
					<view>
						<u-icon name="close" color="#fff" size="20rpx"></u-icon>
					</view>
				</view>
				<view v-if="video.upload" class="success">
					<view>
						<u-icon name="checkmark" color="#fff" size="20rpx"></u-icon>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import VODUpload from './aliyun-vod/aliyun-upload-sdk-1.0.1.min.js'
	export default {
		props: ['propsIndex', 'propsVideo'],
		data() {
			return {
				video: {
					url: null,
					aliyun_video_id: null,
					duration: null,
					upload: false,
					coverUrl: null,
					id: null
				},
				videoUpload: {
					aliyun: null,
				},
				index: 0
			}
		},
		mounted() {
			this.aliyunInit()
			if (this.propsIndex != 1) {
				this.upload()
			}
			if (this.propsVideo?.id) {
				this.video = this.propsVideo
			}
		},
		methods: {
			upload() {
				uni.chooseVideo({
					sourceType: ['album', 'camera'],
					compressed: false,
					maxDuration: 15,
					camera: 'back',
					success: (res) => {
						console.log(res)
						if (res.duration > 15) {
							this.$emit('close')
							this.$u.toast('视频时间不能大于15秒')
							return
						}
						var userData = '{"Vod":{}}'
						var file = {
							url: res.tempFilePath,
							coverUrl: res.thumbTempFilePath
						};
						var vido = {
							url: res.tempFilePath,
							aliyun_video_id: null,
							duration: res.duration,
							upload: false,
							coverUrl: res.thumbTempFilePath
						};
						this.video = vido
						this.$emit('changeVideo', this.video)
						this.videoUpload.aliyun.addFile(file, null, null, null, userData)
						this.videoUpload.aliyun.startUpload();
					},
					fail: () => {
						this.$emit('close')
					}
				})


			},
			aliyunInit() {
				// 阿里云初始化
				this.videoUpload.aliyun = new VODUpload({
					//阿里云的账号ID，不能为空，您可以使用阿里云账号访问账号中心（https://account.console.aliyun.com/），即可查看账号ID。            
					userId: "1386228921490232",
					//上传到点播的地域，默认值为'cn-shanghai'，//eu-central-1，ap-southeast-1，更多信息请参见媒体上传概述
					region: "cn-shanghai",
					//网络原因失败时，重新上传次数，默认为3
					retryCount: 3,
					//网络原因失败时，重新上传间隔时间，默认为2秒
					retryDuration: 2,
					addFileSuccess: function(uploadInfo) {},
					onUploadstarted: (uploadInfo) => {
						this.$api.Aliyun.Video.Create({
							title: '微信视频' + new Date(),
							filename: '微信视频.mp4'
						}).then(res => {
							this.video.aliyun_video_id = res.data.video_id

							this.videoUpload.aliyun.setUploadAuthAndAddress(
								uploadInfo,
								res.data.upload_auth,
								res.data.upload_address,
								res.data.image_id
							);
						})
					},
					onUploadSucceed: (uploadInfo) => {
						let obj = {
							aliyun_video_id: this.video.aliyun_video_id,
							id: this.video.aliyun_video_id,
							title: '微信视频' + new Date(),
							filename: '微信视频.mp4',
							duration: this.video.duration
						}
						this.$api.Video.Add(obj).then(res => {
							uni.$u.toast('上传完成')
							this.$emit('changeVideo', this.video)
							this.video.upload = true
							this.video.id = res.data.video.id
						}).catch(err => {
							uni.$u.toast(err.data.message)
						this.$emit('close')
							this.video = {}
						})
					},
					onUploadFailed: (uploadInfo, code, message) => {
						uploader.stopUpload();
					},
					onUploadprogress: (uploadInfo, totalSize, loadedPercent) => {},
					onUploadTokenExpired: (uploadInfo) => {
						//token超时
						uploader.stopUpload();
					},
				});
			},
			del(id) {
				this.$emit('close')
				this.video = {
					url: null,
					aliyun_video_id: null,
					duration: null,
					upload: false,
					coverUrl: null,
					id: null
				}
				this.$emit('changeVideo', this.video)
				this.$api.Video.Del(id)
			},

		}
	}
</script>

<style lang="scss" scoped>
	.uploadImageBox {
		display: flex;
	}

	.imageAddBox {
		width: 320rpx;
		height: 320rpx;
		background-color: #f4f5f7;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		margin-right: 20rpx;
		margin-bottom: 8px;

		.success {
			position: absolute;
			bottom: 0;
			right: 0;
			display: flex;
			flex-direction: row;
			border-style: solid;
			border-top-color: transparent;
			border-left-color: transparent;
			border-bottom-color: #5ac725;
			border-right-color: #5ac725;
			border-width: 9px;
			align-items: center;
			justify-content: center;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				bottom: -10px;
				right: -10px;
			}
		}

		.del {
			position: absolute;
			top: 0;
			right: 0;
			background-color: #373737;
			height: 14px;
			width: 14px;
			display: flex;
			flex-direction: row;
			border-bottom-left-radius: 100px;
			align-items: center;
			justify-content: center;
			z-index: 3;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				top: 0px;
				right: 0px;
			}
		}

		>video,
		image {
			width: 100%;
			height: 100%;
			object-fit: cover;
		}

		.uploadBox {
			position: absolute;
			background-color: rgba(0, 0, 0, 0.3);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 20rpx;
			color: #fff;
			width: 100%;
			height: 100%;
			z-index: inherit;


		}
	}
</style>